package com.lanhu.designpattern.proxy.dynamicproxy.cglibproxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * author:zhangzg
 * date:2020/11/10 23:29
 */
public class HouseAgent implements MethodInterceptor {

    public Object getInstance(Class<?> clazz){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        //通过create创建代理对象
        return enhancer.create();
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        //通过invokeSupper将调用传递给被代理类，通过源码可以知道生成了代理类的子类
        Object object = methodProxy.invokeSuper(o, objects);
        after();
        return object;
    }

    public void before(){
        System.out.println("寻找房源");
    }

    public void after(){
        System.out.println("处理合同，交付定金");
    }
}
